package homework.hoomework_10.phone;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 朱
 * @date 2019042221:48
 */
public class PhoneDaoHashMap implements PhoneDao {
     HashMap<String,Phone> hashMap=new HashMap<String, Phone>();
    @Override
    public void stock(Phone phone) {
        Set<String> set=hashMap.keySet();
        boolean flag=set.contains(phone.getType());
        if (flag){
            System.out.println("已经含有该手机型号");
        }else {
            hashMap.put(phone.getType(),phone);
            System.out.println("入库成功");
        }
    }

    @Override
    public void select_type(String type) {
        Set<String> set=hashMap.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key= (String) iterator.next();
            Phone value=hashMap.get(key);
            if (value.getType().equals(type)){
                System.out.println(value);
            }else {
                System.out.println("您查找的手机不存在");
            }
        }
    }

    @Override
    public void select_name(String name) {
        Set<String> set=hashMap.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key= (String) iterator.next();
            Phone value=hashMap.get(key);
            if (value.getType().equals(name)){
                System.out.println(value);
            }else {
                System.out.println("您查找的手机不存在");
            }
        }
    }

    @Override
    public void selectAll() {
        Set<String> set=hashMap.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key= (String) iterator.next();
            Phone value=hashMap.get(key);
                System.out.println(value);
        }
    }

    @Override
    public void delete(String type, String name) {
        Set<String> set=hashMap.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key= (String) iterator.next();
            Phone value=hashMap.get(key);
            if (value.getType().equals(type)&&value.getName().equals(name)){
                hashMap.remove(key);
                System.out.println("删除成功");
            }else {
                System.out.println("您输入的手机不存在");
            }
        }
    }
}
